function showImg(){
    layer.photos({
        photos: '.layui-table'
        ,anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
    });
}
layui.use(['form','layer','table','laytpl','laydate'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table,
        laydate = layui.laydate;


    //日期范围
    laydate.render({
        elem: '#dataselect'
        ,range: true
    });

    //用户列表
    var tableIns = table.render({
        elem: '#infoList',
        url : '../info/getInfoList',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        loading:true,
        id : "infoListTable",
        cols : [[
            {field: 'gameName', title: '游戏名称', align:"center"},
            {field: 'typeId', title: '游戏类型', align:'center', templet: function(d){
                    if(d.typeId==1){
                        return "一周热门榜"
                    }else if(d.typeId==2){
                        return "新游畅玩";
                    }else if(d.typeId==3){
                        return "吐血推荐";
                    }else if(d.typeId==4){
                        return "经典必玩";
                    }else if(d.typeId==5){
                        return "狂欢精选";
                    }else if(d.typeId==6){
                        return "全民热玩";
                    }else if(d.typeId==7){
                        return "重点突出";
                    }
                }
            },
            {field: 'cateId', title: '游戏类别', align:'center', templet: function(d){
                    if(d.cateId==1){
                        return "二维码"
                    }else if(d.cateId==2){
                        return "小游戏";
                    }else if(d.cateId==3){
                        return "h5游戏";
                    }
                }
            },{field: 'cateInfo', title: '类别信息', align:'center'},
            {field: 'gameLogo', title: '游戏图标', align:'center',templet: function(d) {
                    return "<img src='"+d.gameLogo+"' onclick='showImg()' class='insImg' width='50px' height='50px' />"
                }},
            {field: 'gameBigLogo', title: '游戏大图', align:'center',templet: function(d) {
                    return "<img src='"+d.gameBigLogo+"' onclick='showImg()' class='insImg' width='50px' height='50px' />"
                }},
            {field: 'count', title: '游戏人数', align:'center'},
            {field: 'state', title: '状态', align:'center', templet: function(d){
                    if(d.state==1){
                        return "正常"
                    }else if(d.state==2){
                        return "下架";
                    }
                }},
            {title: '操作', templet: '#infoListBar', fixed: "right", align: "center"}
        ]]
    });
    //日期范围
    laydate.render({
        elem: '#dataselect'
        , range: true
    });

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
        console.log($(".loginName").val());
        tableIns.reload({
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: { //设定异步数据接口的额外参数，任意设
                gameName:$(".gameName").val(),
                typeId:$(".typeId").val(),
                date:$("#dataselect").val()
            }
        })
    });

    function editType(data) {
        var index = layui.layer.open({
            title: "操作分类",
            type: 2,
            content: "../page/toEditGameInfo?id="+data.id,
            success: function () {
                setTimeout(function () {
                    layui.layer.tips('点击此处返回游戏列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                }, 500)
            }
        });
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize", function () {
            layui.layer.full(index);
        })
    }
    function editState(data) {
        var index = layui.layer.open({
            title: "操作分类",
            type: 2,
            content: "../page/toEditGameState?id="+data.id,
            success: function () {
                setTimeout(function () {
                    layui.layer.tips('点击此处返回游戏列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                }, 500)
            }
        });
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize", function () {
            layui.layer.full(index);
        })
    }

    $(".add_btn").click(function () {
        var index = layui.layer.open({
            title: "操作分类",
            type: 2,
            content: "../page/toAddGameInfo",
            success: function () {
                setTimeout(function () {
                    layui.layer.tips('点击此处返回游戏管理列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                }, 500)
            }
        });
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize", function () {
            layui.layer.full(index);
        })
    });

    function deleteById(data) {
        layer.confirm('确定删除选中的游戏!', {icon: 3, title: '提示信息'}, function (index) {
            console.log(data.id);
            $.ajax({
                url: '../info/deleteById',
                data: {id:data.id},
                method: 'post',
                dataType: 'json',
                success: function (res) {
                    console.log(res)
                    if (res.code == 200) {
                        layer.msg("删除成功");
                        tableIns.reload();
                        layer.close(index);
                    }else {
                        layer.msg("删除失败");
                        layer.close(index);
                    }

                },
                error: function () {
                    layer.msg("系统繁忙,请稍后再试!");
                    layer.close(index);
                }
            });
        });
    }

//列表操作
    table.on('tool(infoList)', function (obj) {
        var layEvent = obj.event,
            //当前行数据
            data = obj.data;

        if (layEvent === 'edit') {
            editType(data);
        }
        if (layEvent === 'state') {
            editState(data);
        }
        if (layEvent === 'delete') {
            deleteById(data);
        }
    });
});